compiled on February 10, 2020
Now let’s plot this data. RchivalTag has several plotting functions for time series data. These include:
hist_tadggboxplot_DepthTS_by_hourplot_DepthTSempty.plot_TSplot_DepthTempTSplot_DepthTempTS_resampledplot_DepthTempTS_resampled_PDTdy_DepthTSIn the first part of this tutorial we have seen how to read and plot histogram data, which can also be obtained from depth time series (DepthTS) data. Although histogram data is an efficient data product to provide an overall overview of general depth layer preferences, its value to analyze diel vertical behavior is limited. On the other side, analyzing DepthTS data from one ore more individual can be tricky, because of the duration and variability in these data sets. Still there are some handy functions in RchivalTag that can help to perform this job, that we will learn in a sec. For now, let’s say we are interested in getting a general idea of the diel vertical behavior depth preferences (which is a good starting point of a more in-depth analysis of DepthTS data). For this purpose, RchivalTag includes the function ggboxplot_DepthTS_by_hour.
ts_file <- system.file("example_files/104659-Series.csv",package="RchivalTag")
ts_df <- read_TS(ts_file)
ggboxplot_DepthTS_by_hour(ts_df)Let’s add position data to obtain twilight and nighttime shadings. We could add here fixed position values.
ts_df$Lon <- 5; ts_df$Lat <- 43
ts_df2 <- get_DayTimeLimits(ts_df)
ggboxplot_DepthTS_by_hour(ts_df2,ylim=c(0,100))However, it is more accurate to use the maximum likelihood positions from the GPE3 model runs, especially if the animal is highly migratory and thus moving over large distances. This is particularly important when we analyze the raw depth time series data. By contrast, the ggboxplot_DepthTS_by_hour function will calculate the average timing of sunrise and sunset.
We can read in the maximum likelihood positions via the get_geopos function. We will learn more about this function and the visualizing of the tracks in the 4th part of this tutorial.
library(dplyr)
gpe3_file <- system.file("example_files/15P1019-104659-1-GPE3.csv",package="RchivalTag")
tracks <- get_geopos(gpe3_file)
## Loading tagging tracks from file: /home/work/R/x86_64-pc-linux-gnu-library/3.6/RchivalTag/example_files/15P1019-104659-1-GPE3.csv
add <- tracks %>% select(DeployID,Ptt,Lat,Lon,date) %>%
group_by(DeployID,Ptt,date) %>%
summarise(Lat=mean(Lat),Lon=mean(Lon)) %>%
mutate(datetime=RchivalTag:::.date2datetime(date))
add <- classify_DayTime(add) %>% select(-datetime)
dat <- ts_df %>% select(-Lon,-Lat)
ts_df2 <- add %>% select(-DeployID) %>% inner_join(dat,by=c("date","Ptt","DeployID"))
ggboxplot_DepthTS_by_hour(ts_df2,ylim=c(0,100))Let’s add the actual depth records on top of the boxplot (only meaningful in case of few amounts of data, low resolution DepthTS):
Now let’s checkout the underlying, raw depth time series data. We can directly plot the entire data set with the
plot_DepthTS function:
This function has a bunch of arguments that we will go through. Some basic arguments like `xlim``to select the dates to be plotted:
Another way to produce this figure is via the empty.plotTS function that will draw an empty plot first and then gives us a line.
### example for empty.plotTS and adding time series data as line:
empty.plot_TS(xlim="2016-08-10",ylim=c(100,0))
lines(ts_df$datetime, ts_df$Depth)Many aquatic animals show diurnal migration patterns so we might be interested in shading the night-time and twilight periods. To do so we need to add at least the Longitude and Latitude information since the sunrise and sunset timings change differ with location. Let’s do this manually.
### plot also day night time information:
ts_df$Lon <- 5; ts_df$Lat <- 43 # manual example, please take Lon/Lat data from (GPE3) model outputs for your analysis instead. (check get_geopos-function from RchivalTag)
ts_df <- classify_DayTime(ts_df)
plot_DepthTS(ts_df,xlim = c("2016-08-10","2016-08-15"),plot_DayTimePeriods = T)We can do the same figure with an empty plot and the vertical track as line on top:
### alternative:
plot_DepthTS(ts_df, xlim=c("2016-08-10","2016-08-12"), plot_DayTimePeriods = TRUE, type='n')
lines(ts_df$datetime, ts_df$Depth)The dy_DepthTS function allows us to to the same figures but with the interactive interface of the dygraphs package.
ts_file <- system.file("example_files/104659-Series.csv",package="RchivalTag")
ts_df <- read_TS(ts_file)
ts_df$Serial <- ts_df$DeployID
dy_DepthTS(ts_df)Adding night and twilight shadings:
Some further arguments:
dy_DepthTS(ts_df, xlim = unique(ts_df$date)[2:3], plot_DayTimePeriods = FALSE, doRangeSelector= FALSE)Remove grid, plot points:
RchivalTag fills gaps with NAs. While plot_DepthTS can also deal with data missing data gaps, dy_DepthTS connects subsequent points, ignoring subsequent records, including data gaps:
ts_gaps <- ts_df
ts_gaps$Depth[c(300:800)] <- NA
ts_cut <- ts_df[-c(300:800),]
plot_DepthTS(ts_gaps)
plot_DepthTS(ts_cut) # same resultplot_DepthTS is a base-graph. So it comes with almost all the functionalities of the base plots. We could for example also add another line and axis for the temperature data.
par(mar=c(5,4,4,5)) ## change margins to add second axis label
ts_sub <- plot_DepthTS(ts_df,xlim = c("2016-08-10","2016-08-15"),plot_DayTimePeriods = T, Return=T)
par(new=T) ## plot on top of first depth time series plot
ylim <- range(pretty(range(ts_sub$Temperature)))
plot(ts_sub$datetime, ts_sub$Temperature, xlim=range(ts_sub$datetime), ylim = ylim, type='n',axes=F,xlab="",ylab="") # create empty plot
lines(ts_sub$datetime, ts_sub$Temperature,col="orange") # add line
axis(4, at = pretty(ylim)) # add axis
mtext(side=4,"Temperature (°C)",las=0,line=2.3) # add axis labelLet’s create a second line for the temperature time series data with dy_DepthTS and dygraphs:
dg <- dy_DepthTS(ts_df) # run via dy_DepthTS to get shadings
## manually create the same figure with a second line
dat <- ts_df[,c("datetime","Depth","Temperature")]
dat$datetime <- as.POSIXct(dat$datetime,tz = "UTC")
dat_xts <- xts::xts(dat[,2:3],order.by=dat$datetime)
dg2 <- dygraph(dat_xts) # create initial dygraph object
dg2$x$shadings <- dg$x$shadings ## assign shadings
# define limits of y-axes
ylim1 <- c(max(ts_df$Depth,na.rm=T),-1)
ylim2 <- c(12,30)
dg2 <- dg2 %>% dyRangeSelector() %>%
dySeries("Temperature", axis = "y2") %>% ## set up second y-axis
dyOptions(labelsUTC = TRUE, strokeWidth = 1) %>% ## set datetime format to UTC (default is local time zone)
dyAxis("y", label = "Depth", valueRange = ylim1) %>% ## set limits and label of y1-axis
dyAxis("y2", label = "Temperature", valueRange = ylim2) ## set limits and label of y2-axis
dg2Well this worked, but it was quite tricky and it’s not that pretty either.
RchivalTag has some additional functions to illustrate depth-temperature time series data in a much nicer way.
One option is plot_DepthTempTS. In this case, adjacent depth-temperature records will be linearly interpolated.
The outcome looks good, but is not recommended for low resolution time series data (>= 300 s sampling resolution), because the potential interpolation between distant records might be misleading (e.g. 30 degrees at the surface and 10 degrees at 400m leads to a false temperature profile, with 20 degrees at around 200 m)… .
library(oceanmap)
data(cmap)
par(mar=c(5,4,4,0)) ## change margins back to default
plot_DepthTempTS(ts_df,xlim = c("2016-08-10","2016-08-15"))Another option is to interpolate the temperature based on daily resamples (average profiles) via the plot_DepthTempTS_resampled.
Both of these functions are adaptations of plot_DepthTS, which means we have similar arguments (e.g. to produce night-time and twilight shadings).
plot_DepthTempTS_resampled(ts_df,xlim = c("2016-08-10","2016-08-15"),plot_DayTimePeriods = T)
## [1] "2016-08-07"
## [1] "2016-08-08"
## [1] "2016-08-09"
## [1] "2016-08-10"
## [1] "2016-08-11"
## [1] "2016-08-12"
## [1] "2016-08-13"
## [1] "2016-08-14"
## [1] "2016-08-15"
## [1] "2016-08-16"
## [1] "2016-08-17"
## [1] "2016-08-18"
## [1] "2016-08-19"
## [1] "2016-08-20"
## [1] "2016-08-21"Another option is to estimate the temperature profiles from external data sets. PDT-data (PAT-Style Depth-Temperature profiles) from Wildlife computers are one example for that.
ts_df$Temperature <- c()
pdt_file <- system.file("example_files/104659-PDTs.csv",package="RchivalTag")
PDT <- read_PDT(pdt_file)
## running pdt_file /home/work/R/x86_64-pc-linux-gnu-library/3.6/RchivalTag/example_files/104659-PDTs.csv
plot_DepthTempTS_resampled_PDT(ts_df,PDT,xlim = c("2016-08-10","2016-08-15"),plot_DayTimePeriods = T)
## [1] "station 1 - set: 2016-08-07"
## [1] "station 1 - set: 2016-08-08"
## [1] "station 1 - set: 2016-08-09"
## [1] "station 1 - set: 2016-08-10"
## [1] "station 1 - set: 2016-08-11"
## [1] "station 1 - set: 2016-08-12"
## [1] "station 1 - set: 2016-08-13"
## [1] "station 1 - set: 2016-08-14"
## [1] "station 1 - set: 2016-08-15"
## [1] "station 1 - set: 2016-08-16"
## [1] "station 1 - set: 2016-08-17"
## [1] "station 1 - set: 2016-08-18"
## [1] "station 1 - set: 2016-08-19"
## [1] "station 1 - set: 2016-08-20"
## [1] "station 1 - set: 2016-08-21"We will discuss this the different types of temperature profiles in the 3rd part of this tutorial.
```